package com.epoint.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.epoint.bizlogic.activityinfo.domain.ActivityInfo;
import com.epoint.bizlogic.activityinfo.domain.ActivityInfoVo;
import com.epoint.utils.JDBCUtil;
import com.epoint.utils.SQLUtil;
import com.epoint.utils.template.delete.MySqlDelete;
import com.epoint.utils.template.insert.MySqlInsert;
import com.epoint.utils.template.query.MySqlQueryPageBlur;

/**
 * 活动信息
 * @author 
 *
 */
public class ActivityInfoDao
{

    /**
     * 查询活动信息 
     * @param pageIndex 页号
     * @param pageSize 页面大小
     * @return
     */
    public List<ActivityInfoVo> queryActivityInfo(Integer pageIndex, Integer pageSize) {
        Map<String, Object> paramFieldMap = new LinkedHashMap<>();
        StringBuffer sql = new StringBuffer();
        sql.append("select a.*, c.clubname from activityinfo a left join clubinfo c on a.clubid = c.clubid  where 1 = 1 ");
        if (pageIndex != null && pageSize != null) {
            sql.append(" limit ?, ? ");
            paramFieldMap.put("Integer_1", pageIndex * pageSize);
            paramFieldMap.put("Integer_2", pageSize);
        }
        MySqlQueryPageBlur<ActivityInfoVo> queryPageBlur = new MySqlQueryPageBlur<>();
        return queryPageBlur.executeSql(sql.toString(), ActivityInfoVo.class, paramFieldMap);
    }

    /**
         * 获取活动id
     * @param clubId id
     * @return
     */
    public String getActivityId(String clubId) {
        String sql = "select concat(year(now()),lpad(right(max(right(activityid, 7)), 3) + 1, 3, 0)) from activityinfo where clubid = ?";
        
        Connection con = JDBCUtil.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String activityId = null;
        
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, clubId);
            System.out.println("getActivityId->" + ps);
            rs = ps.executeQuery();
            while(rs.next()) {
                activityId = rs.getString(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.closeConnection(rs, ps, con);
        }
        return activityId;
    }

    /**
         * 新增活动信息 
     * @param activityInfo 活动信息
     * @return 条数
     */
    public int addActivityInfo(ActivityInfo activityInfo) {
        String clubId = activityInfo.getClubId();
        Integer isFree = activityInfo.getIsFree();
        Integer partNum = activityInfo.getPartNum();
        
       String sql1 = "insert into activityinfo (activityid,clubid,activityname,publicplace,publictime,isfree,partnum) "
               + "values (?,?,?,?,?,?,?)";
       String sql2 = "update clubinfo set remain = (remain - ?) where clubid = ?";
       
       Connection con = JDBCUtil.getConnection();
       PreparedStatement ps = null;
       String activityId = null;
       
       try {
           con.setAutoCommit(false);
           ps = con.prepareStatement(sql1);
           ps = SQLUtil.insertObjectInfo(activityInfo, ps);
           System.out.println("addActivityInfo->sql1" + ps);
           int i = ps.executeUpdate();
           
           // 不免门票不需要减去余票数
           if (isFree == 1) {
               con.commit();
               ps.close();
               return i;
           } else {
               if (i > 0) {
                   ps = con.prepareStatement(sql2);
                   ps.setInt(1, partNum);
                   ps.setString(2, clubId);
                   System.out.println("addActivityInfo->sql2" + ps);
                   int j = ps.executeUpdate();
                   if (i > 0 && j > 0) {
                       con.commit();
                       return 1;
                   } else {
                       con.rollback();
                       return 0;
                   }
                   
               } else {
                  return i;
               }
           }
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           JDBCUtil.closeConnection(null, ps, con);
       }
       return 0;
    }

    /**
     * 删除活动信息
     * @param activityId
     * @return
     */
    public int deleteActivityInfo(String activityId) {
        String sql = "delete from activityinfo where activityid = ?";
        MySqlDelete sqlDelete = new MySqlDelete();
        return sqlDelete.executeSql(sql, activityId);
    }
}
